/**
 * 
 */
package com.mie.consumer;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Consumer;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;

/**
 * @author Mie
 *
 */
public class Consumer_HelloWorld {
	 public static void main(String[] args) throws IOException, TimeoutException {
		//1.create factory
		 ConnectionFactory factory=new ConnectionFactory();
		 //2.set parameters
		 factory.setHost("localhost");
		 factory.setPort(5672);
		 factory.setVirtualHost("/");
		 factory.setUsername("rollen");
		 factory.setPassword("root");
		 //3.create connection
		 Connection connection=factory.newConnection();
		 //4.create channel
		 Channel channel=connection.createChannel();
		 //5.declare queue
		 channel.queueDeclare("hello world!", true, false, false, null);
		 //6.receive message
		 Consumer consumer=new DefaultConsumer(channel) {

			@Override
			public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
					throws IOException {
				System.out.println("consumerTag:"+consumerTag);
				System.out.println("envelope:"+envelope.getExchange());
				System.out.println("properties:"+properties);
				System.out.println("body:"+new String(body));
			}
			 
		 };
		 channel.basicConsume("hello world!", true, consumer);
		 
	}

}
